Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
In console I see "undefined" instead of 25. Why? How to see the correct result?

function Square(a) {
  this.a = a ** 2;
  return a;
}

const value = Square.call({}, 5);
console.log(value.a);

Actual -> value.a = undefined

Expected -> value.a = 25

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are returning a, so 5, not the object. And you pass an object {} for this, but it is not stored anywhere, you can't check it later. If you passed an object which you have access to afterwards, like diz below, its .a becomes 25 properly:

function Square(a) {
  this.a = a ** 2;
  return a;
}

const diz = {};
const value = Square.call(diz, 5);
console.log("Returned 'a', should be 5:",value);
console.log("Altered 'object.a', should be 25:",diz.a);

Alternatively you might have wanted to return this;:

function Square(a) {
  this.a = a ** 2;
  return this;
}

const value = Square.call({}, 5);
console.log(value.a);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I don't know why you use this, or where this object value is coming from. But let me explain. Your argument a can't be changed directly. You need to assign it to a variable, e.g. result to change and return it.

return result = a ** 2;

To call the function, you don't use .call. You simply call it in your console.log().

console.log(square(a));

The whole magic of this function you want to write:

function square(a) {
    return result = a * a;
}

console.log(square(a));

// expected result is 25

I recommend a course on freeCodeCamp to learn the fundamentals of JavaScript. You can find it here.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda